home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 67 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  121 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: PLEASE PLEASE...THANKS THANKS!!
  5. Date: 1 Jan 1996 15:25:34 GMT
  6. Organization: The Internet Access Group, Inc.
  7. Message-ID: <4c8ude$lut@news.iag.net>
  8. References: <1995Dec31.061049.16566@wvnvms>
  9. NNTP-Posting-Host: pm2-orl21.iag.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.6
  13.  
  14. In article <1995Dec31.061049.16566@wvnvms>, un025043@wvnvms.wvnet.edu says...
  15. >
  16. >Thanks a lot for that stuff on state machines!
  17. >As two of you pointed out, using fork or spawn is an alternative,
  18. >nut that makes it os-dependent.
  19. >Also, as you very wisely remarked, it is not possible to control
  20. >the numnber of instrcutions executed, but only the timeslice.
  21. >
  22. >The only choice left seemed to be that of using a state machine model,
  23. >having static ints in each procedure & using as many "cases" for the switch
  24. >as the number of statements.
  25. >But you see, the solution is quite not as simple...
  26. >if I had 4 STATEMENTS, i could intersperse them with four switch cases,
  27. >but since I have a LOOP, where do I put the switch cases?
  28. >I mean, you can't expand a conditional loop and interleave each statement
  29. >with case statements, right?
  30. >
  31. >If you can tackle this minor(major) hitch, please let me know.
  32. >I shall continue to think on the lines of the "state model".
  33.  
  34. Can you fake the loop by using one or more additional static ints for 
  35. iteration control? Something like:
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. int func1( void)
  41.    {
  42.    static int step=0, iter=0;
  43.    switch( step)
  44.       {
  45.       case 0:
  46.          puts( "func1, step0");
  47.          step++;
  48.          break;
  49.  
  50.       case 1:
  51.          if( iter != 4)
  52.             printf( "func1, step1, iter%d\n", iter++);
  53.          else
  54.             {
  55.             step++;
  56.             iter=0;
  57.             }
  58.          break;
  59.  
  60.       case 2:
  61.          puts( "func1, step2");
  62.          step++;
  63.          break;
  64.  
  65.       default:
  66.          return 0;
  67.       }
  68.    return 1;
  69.    }
  70.  
  71. int func2( void)
  72.    {
  73.    static int step=0, iter=0;
  74.    switch( step)
  75.       {
  76.       case 0:
  77.          puts( "func2, step0");
  78.          step++;
  79.          break;
  80.  
  81.       case 1:
  82.          if( iter != 2)
  83.             printf( "func2, step1, iter%d\n", iter++);
  84.          else
  85.             {
  86.             step++;
  87.             iter=0;
  88.             }
  89.          break;
  90.  
  91.       case 2:
  92.          puts( "func2, step2");
  93.          step++;
  94.          break;
  95.  
  96.       default:
  97.          return 0;
  98.       }
  99.    return 1;
  100.    }
  101.  
  102.  
  103. int main()
  104.    {
  105.    int f1stat=1, f2stat=1;
  106.  
  107.    while( f1stat || f2stat)
  108.       {
  109.       if( f1stat)
  110.          f1stat = func1();
  111.       if( f2stat)
  112.          f2stat = func2();
  113.       }
  114.    return 0;
  115.    }
  116.  
  117. -- 
  118. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  119. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  120.  
  121.